# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @Project      :commercial_opportunity_zhu
# @FileName     :LoginChangeUser.py
# @Time         :2021-11-11 14:02:52
# @Author       :@HaoWei
# @Desc         :切换用户，在配置更换用户可进行切换


from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from main_process import TaskPublic
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from PubilcTask.Common.CommonBusiness import CommonBusiness


class LoginRegister(TaskPublic.TaskPublic):

    def login_pc(self):
        """
        定位登陆按钮
        5s之内，每隔0.5s检测一次元素。直到查找到
        :return:
        """
        element = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'loginBtn')))
        element.click()
        self.logger.info("定位第一个登陆")

    def choice_common(self):
        """
        默认取配置
        :param :
        :return:
        """
        element = WebDriverWait(self.b, 5, 0.5).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#rblIsDomainUser > label:nth-child(4)')))
        element.click()
        self.logger.info("选择普通用户成功")

    def input_text(self, user=TaskPublic.user):
        """
        默认取配置
        :param user: 用户名可修改、
        :return:
        """

        element = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'userid')))
        element.send_keys(user)
        self.logger.info("输入用户名成功")

    def input_password(self, password=TaskPublic.password):
        """
        默认取配置
        :param password: 密码可修改
        :return:
        """
        element = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'passwordPS')))
        element.send_keys(password)
        self.logger.info("输入密码成功")

    def change_user(self, user=TaskPublic.change):
        """
        :param 切换用户
        :return:
        """

        CommonBusiness.Change_frame_head(self)
        self.b.find_element_by_id('ChangeUser').click()
        self.b.switch_to.default_content()
        self.b.switch_to.frame(self.b.find_element_by_css_selector(
            '#ymBSF-ml > div > div > div > div > table > tbody > tr:nth-child(2) > td:nth-child(2) > div > iframe'))
        element = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'txtName')))
        element.send_keys(user)
        element0 = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'btnSearchClient')))
        element0.click()
        element1 = WebDriverWait(self.b, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'txtRemarks')))
        element1.send_keys('1')
        sleep(2)
        one = self.b.find_element_by_id('grdEmployee_row_0')
        ActionChains(self.b).double_click(one).perform()
        self.b.switch_to.alert.accept()
        sleep(3)
        self.logger.info("切换用户success")


if __name__ == "__main__":
    run_code = 0
